<?php

declare (strict_types=1);

namespace App\Model;

/**
 * @property int $id
 * @property int $cid 分类ID
 * @property string $name 属性名
 * @property int $sequence 排序
 * @property string $create_time
 * @property string $update_time
 */
class GProperty extends Model {
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'g_property';

    /**
     * 根据分类ID获取属性
     * @param $cid
     * @return \Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection
     */
    public static function getProperties($cid) {
        $properties = self::query()
            ->where('cid', $cid)
            ->orderByDesc('sequence')
            ->orderBy('id')
            ->select('id', 'name')
            ->get();
        if (!$properties->isEmpty()) {
            $values = GPropertyValue::getValues($cid);
            foreach ($properties as $property) {
                $property->addBinding('values', []);
                foreach ($values as $value) {
                    if ($property->id == $value->pid) {
                        $property->values[] = $value;
                    }
                }
            }
        }
        return $properties;
    }
}